package hu.tortavar2.client.view.adminpages;

import java.util.List;

import hu.tortavar2.shared.ProductCategory;
import hu.tortavar2.shared.Shop;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class AddNewProductPageImpl extends Composite implements AddNewProductPage {

	private static AddNewProductPageImplUiBinder uiBinder = GWT
			.create(AddNewProductPageImplUiBinder.class);

	interface AddNewProductPageImplUiBinder extends
			UiBinder<Widget, AddNewProductPageImpl> {
	}

	Presenter presenter;
	
	@UiField Anchor addNewProductButton;
	@UiField Grid table;
	@UiField FormPanel addNewProductForm;
	@UiField HTMLPanel grid_02_left;
	//@UiField HTMLPanel grid_02_right;
	@UiField HTMLPanel padding_color_left;
	//@UiField HTMLPanel padding_color_right;
	@UiField ListBox productCategoryListBox;
	
	public AddNewProductPageImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		setFields();
	}

	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@UiHandler("addNewProductButton")
	void saveProduct(ClickEvent e) {
		presenter.onAddNewProductClicked();
	}
	
	private void setFields() {
		addNewProductButton.getElement().setClassName("button");
		addNewProductForm.setWidget(table);
		addNewProductButton.setName("addNewProductForm");
		addNewProductForm.setMethod(FormPanel.METHOD_POST);
		addNewProductForm.setEncoding(FormPanel.ENCODING_MULTIPART);
//		addNewProductForm.setAction(GWT.getModuleBaseURL() + "uploadservlet");
		grid_02_left.getElement().setClassName("grid_02");
//		grid_02_right.getElement().setClassName("grid_02");
		padding_color_left.getElement().setClassName("padding pink");
//		padding_color_right.getElement().setClassName("padding pink");
	}

	public void submitAddNewProductForm() {
		addNewProductForm.submit();
	}

	@Override
	public void setCategory(List<ProductCategory> categories) {
		for(ProductCategory category : categories) {
			productCategoryListBox.addItem(category.getCategoryLongName(), category.getCategoryShortName());
		}
	}

	@Override
	public void setBlobstoreUploadUrl(String uploadUrl) {
		addNewProductForm.setAction(uploadUrl);
	}
}
